package com.app.babygo;

import com.app.babygo.dbservice.ChildAdapter;
import com.app.babygo.exceptions.InternalDBException;
import com.app.babygo.help.ActionHelper;
import com.app.babygo.help.MessageHelper;
import com.app.babygo.obj.Child;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class ChildCreation extends Activity {
	
	final public static String CHILD_ID_KEY = "ChildID";
	
	protected static String PicPath = null;
	protected Boolean isTakingPic = false;
	protected String childID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_child_creation);
		PicPath = null;
		childID = ActionHelper.ChildIdGenerator();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.child_creation, menu);
		return true;
	}
	
	public void takePicture(View view) {
		isTakingPic = true;
		Intent intent = new Intent(this, ChildTakePicture.class);
		intent.putExtra(CHILD_ID_KEY, childID);
		startActivity(intent);
	}

	public void setBabyProfile(View view) {
		if (PicPath == null) {
			//MessageHelper.showMsg(this, "Infomation", "Please take a pictrue for your child first.");
			//return;
			PicPath = "NO_Picture_Taken";
		}
		EditText childname_box = (EditText) findViewById(R.id.child_name);
		String childname = childname_box.getText().toString();
		EditText childage_box = (EditText) findViewById(R.id.child_age);
		int childage = Integer.parseInt(childage_box.getText().toString());
		EditText childescrip_box = (EditText) findViewById(R.id.child_info);
		String childescrip = childescrip_box.getText().toString();
		
		int child_gender = -1;
		TextView childgender = (TextView) findViewById(R.id.childgender);
		if(childgender.getText().toString().equalsIgnoreCase("Boy")){
			child_gender = 1;
		} else if(childgender.getText().toString().equalsIgnoreCase("Girl")){
			child_gender = 2;
		} else {
			MessageHelper.showMsg(this, "Child infomation error","Please choose child gender");
			return;
		}
		
		if (childname.equalsIgnoreCase("")) {
			MessageHelper.showMsg(this, "Child infomation error","Contain empty field");
			return;
		}
		
		Child newChild = new Child(
			childID,
			childname, 
			ActionHelper.GetCurrentUserId() , 
			childage,
			child_gender,
			childescrip,
			PicPath
		);
		ChildAdapter adapter = new ChildAdapter(this);
		try {
			adapter.open();
			adapter.addChild(newChild);
		} catch (InternalDBException e) {
			MessageHelper.showMsg(this, "Error", e.getMessage());
			Log.e("TestError", e.getMessage());
		}
		finally
		{
			adapter.close();
		}
		Intent intent = new Intent(this, ChildChoose.class);
		startActivity(intent);
		PicPath = null;
	}
	
	protected void onPause() {
		super.onPause();
		if (!isTakingPic) {
			// TODO: clean up picture file if PicPath != null
			finish();
		}
	}
	
	protected void onResume() {
		super.onResume();
		isTakingPic = false;
	}
	
	public boolean ChildIngoValidation(Child childinfo) {
		
		//TODO; Blocked by ActionHelper functions
		boolean childnameVal = ActionHelper.StringValidation(childinfo.getChildname());
		boolean childinfoVal = ActionHelper.StringValidation(childinfo.getChildinfo());
		
		return true;
	}
	
	public void setGirl(View view){
		RadioButton rd_boy = (RadioButton) findViewById(R.id.radio_boy);
		rd_boy.setChecked(false);
		TextView childgender = (TextView) findViewById(R.id.childgender);
		childgender.setText(R.string.childgender_girl);
	}
	
	public void setBoy(View view){
		RadioButton rd_girl = (RadioButton) findViewById(R.id.radio_girl);
		rd_girl.setChecked(false);
		TextView childgender = (TextView) findViewById(R.id.childgender);
		childgender.setText(R.string.childgender_boy);
	}
	
	public static void setPicturePath(String path){
		PicPath = path;
	}

}
